<template>
  <div>
    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="searchForm.shelfName" placeholder="货架名称" clearable />
      </el-form-item>

      <el-form-item label="">
        <el-input v-model="searchForm.areaName" placeholder="所属库区" clearable />
      </el-form-item>
      <el-form-item label="">
        <el-select label="货架类型" v-model="searchForm.type" >
            <el-option label="货架" value="1"></el-option>
            <el-option label="货架层" value="2"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增 -->
    <div style="display: inline;">
      <el-button type="primary" round @click="handleAddShelf">新增货架</el-button>
      <el-dialog v-model="addDialogVisible" title="新增货架信息" width="500">
        <el-form :model="newShelf">
          <el-form-item label="货架名称" :label-width="formLabelWidth">
            <el-input v-model="newShelf.shelfName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架代码" :label-width="formLabelWidth">
            <el-input v-model="newShelf.shelfCode" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架层数" :label-width="formLabelWidth">
              <el-input v-model="newShelf.level" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="所属库区" :label-width="formLabelWidth">
            <el-input v-model="newShelf.areaName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架长宽高" :label-width="formLabelWidth">
            <div>
              <el-input v-model="newShelf.length" placeholder="长" style="width: 100px;" autocomplete="off" />
              <el-input v-model="newShelf.width" placeholder="宽" style="width: 100px;" autocomplete="off" />
              <el-input v-model="newShelf.high" placeholder="高" style="width: 100px;" autocomplete="off" />
            </div>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="addDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleAddConfirm">确认</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <!-- 新增货架层 -->
    <div style="display: inline;">
      <el-button type="primary" round @click="handleAdd">新增货架层</el-button>
      <el-dialog v-model="addDialog" title="新增货架层信息" width="500">
        <el-form :model="newShelf">
          <el-form-item label="货架名称" :label-width="formLabelWidth">
            <el-input v-model="newShelf.shelfName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架代码" :label-width="formLabelWidth">
            <el-input v-model="newShelf.shelfCode" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架层数" :label-width="formLabelWidth">
              <el-input v-model="newShelf.level" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="所属货架" :label-width="formLabelWidth">
            <el-input v-model="newShelf.parentName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架长宽高" :label-width="formLabelWidth">
            <div>
              <el-input v-model="newShelf.length" placeholder="长" style="width: 100px;" autocomplete="off" />
              <el-input v-model="newShelf.width" placeholder="宽" style="width: 100px;" autocomplete="off" />
              <el-input v-model="newShelf.high" placeholder="高" style="width: 100px;" autocomplete="off" />
            </div>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="addDialog = false">取消</el-button>
            <el-button type="primary" @click="handleAddConfirm">确认</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <!-- 导出按钮 -->
    <download-excel download-excel
            class = "export-excel-wrapper"
            :data = "exportData"
            name = "download.xls">
            <el-button type="warning" round @click="handleExport">导出数据</el-button>
        </download-excel>
    <div style="height: 20px;"></div>

    <!-- 数据展示区域 -->
    <el-table :data="tableData.resultList" border style="width: 100%" max-height="500">
      <el-table-column prop="shelfId" sortable label="序号"  />
      <el-table-column prop="shelfName" sortable label="名称"  />
      <el-table-column prop="shelfCode" sortable label="代码" />
      <el-table-column sortable label="所属(库区)货架">
        <template #default="scope">
          {{ scope.row.areaName || scope.row.parentName || '-' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" sortable label="创建时间" />
      <el-table-column prop="createName" sortable label="创建人" />
      <el-table-column prop="updateTime" sortable label="修改时间" />
      <el-table-column prop="updateName" sortable label="修改人" />
      
      <!-- 操作列 -->
      <el-table-column fixed="right" label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>

          
        </template>
      </el-table-column>
    </el-table>

    <!-- 信息修改 -->
    <el-dialog v-model="editDialogVisible" title="编辑装卸点信息" width="500">
      <el-form :model="editForm">
        <el-form-item label="货架名称" :label-width="formLabelWidth">
            <el-input v-model="editForm.shelfName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架代码" :label-width="formLabelWidth">
            <el-input v-model="editForm.shelfCode" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架层数" :label-width="formLabelWidth">
              <el-input v-model="editForm.level" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item v-if="editForm.type === '1'" label="所属库区" :label-width="formLabelWidth">
            <el-input v-model="editForm.areaName" autocomplete="off" />
          </el-form-item>
          <el-form-item v-if="editForm.type === '2'" label="所属货架" :label-width="formLabelWidth">
            <el-input v-model="editForm.parentName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="货架长宽高" :label-width="formLabelWidth">
            <div>
              <el-input v-model="editForm.length" placeholder="长" style="width: 100px;" autocomplete="off" />
              <el-input v-model="editForm.width" placeholder="宽" style="width: 100px;" autocomplete="off" />
              <el-input v-model="editForm.high" placeholder="高" style="width: 100px;" autocomplete="off" />
            </div>
          </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleEditConfirm">确认</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 分页区域 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="searchCondition.pageNum"
        v-model:page-size="searchCondition.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'  // 添加 onMounted 引入
import { searchAreaName,searchShelfName,  searchShelf, addShelf, deleteShelf, updateShelf} from '../api/api.js'
import { ElMessage } from 'element-plus'

// ==============================

// 表格数据对象
const tableData = ref(
 [], // 当前页数据列表
)

// 对话框显示状态
const addDialogVisible = ref(false)    // 新增对话框
const addDialog = ref(false)    // 新增对话框
const editDialogVisible = ref(false)   // 编辑对话框
const formLabelWidth = '120px'         // 表单标签宽度

// 搜索表单数据
const searchForm = reactive({
  shelfName: '',   
  areaName: '',
  type: ''
})

// 新增表单数据
const newShelf = reactive({
  shelfName: '',     
  shelfCode: '',      
  areaName: '',         
  type: '',
  parentName: '',
  length: '',
  width: '',
  high: '',
  level: '',   
  state: '',

})

// 编辑表单数据
const editForm = reactive({
  shelfName: '',     
  shelfCode: '',      
  areaName: '',         
  type: '',
  parentName: '',
  length: '',
  width: '',
  high: '',
  level: '',   
  state: '',  
})



const searchCondition = {
  shelfName: '',   
  areaName: '',   
  type: '',
  pageNum: 1,      
  pageSize: 10     
}

// ==============================

/**
 * 查询列表
 */
const search = async () => {
  searchCondition.shelfName = searchForm.shelfName
  searchCondition.areaName = searchForm.areaName
  searchCondition.type = searchForm.type

  const res = await searchShelf(searchCondition)
  if(res.data.code === 200) {
    ElMessage.success('查询成功')
  } else {
    ElMessage.error('查询失败：' + res.data.message)
  }
  tableData.value = res.data.data
  console.log(tableData.value)

}


const handleAddShelf = () => {
  Object.keys(newShelf).forEach(key => {
    newShelf[key] = ''
  })
  newShelf.type = '1'
  newShelf.state = '1'
  addDialogVisible.value = true
}

const handleAdd = () => {
  Object.keys(newShelf).forEach(key => {
    newShelf[key] = ''
  })
  newShelf.type = '2'
  newShelf.state = '1'
  addDialog.value = true
}



/**
 * 新增
 */
const handleAddConfirm = async () => {

    addShelf(newShelf)
    search()
    addDialog.value = false
    addDialogVisible.value = false
}

/**
 * 编辑对话框
 */
const handleEdit = (row) => {
  Object.assign(editForm, row)
  editDialogVisible.value = true
}

/**
 * 编辑确认提交
 */
const handleEditConfirm = async () => {
 
    await updateShelf(editForm)
    search()
    editDialogVisible.value = false

}

/**
 * 删除
 */
const handleDelete = async (row) => {
    await deleteShelf(row.shelfId)
    search()

}


/**
 * 改变每页显示数量
 */
const handleSizeChange = (val) => {
  
  search()
}

/**
 * 改变当前页码
 */
const handleCurrentChange = (val) => {

  search()
}

//更新状态
const toggleStatus = (row) => {
  row.state = row.state === '1' ? '0' : '1'
  updatePosition(row).then(() => {
    search()
  })
}

const exportData = ref([])

/**
 * 导出数据
 */
const handleExport = () => {
  exportData.value = tableData.value.resultList
  console.log(exportData)
  console.log('导出数据')
}

onMounted(() => {
  search()
})
</script>

<style scoped>
 .demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.operation-area {
  margin: 15px 0;
}

.pagination-area {
  margin-top: 20px;
  text-align: right;
}
</style>
